<?php

namespace App\Model\Api;

use App\Model\BaseModel;
use Illuminate\Support\Facades\DB;

class RoomLivePlan extends BaseModel
{
    protected $connection = 'api';
    protected $table = "room_live_plan";
    private $fields = ['*'];

    public function getStartTimeAttribute($startTime) {
        return date('Y-m-d H:i:s', $startTime);
    }

    public function getEndTimeAttribute($endTime) {
        return date('Y-m-d H:i:s', $endTime);
    }

    public function createOrUpdateData(array $data = [], int $id = 0) {
        return curl_request(getKkaudioApiUrl().'/inner/room_live/store', [
            'id' => $id ?? 0,
            'platform_id' => $data['platform_id'] ?? 0,
            'host_id' => $data['host_id'] ?? 0,
            'price' => $data['price'] ?? 0,
            'start_time' => $data['start_time'] ? strtotime($data['start_time']) : 0,
            'end_time' => $data['end_time'] ? strtotime($data['end_time']) : 0,
            'room_id' => $data['room_id'] ?? 0,
            'follow_id' => $data['follow_id'] ?? 0,
            'remark' => $data['remark'] ?? '',
            'host_name' => $data['host_name'] ?? '',
            'type' => $data['type'] ?? 0,
            'follow_name' => $data['follow_name'] ?? ''
        ]);
    }

    public function deleteData($id) {
        return curl_request(getKkaudioApiUrl().'/inner/room_live/remove', [
            'id' => $id
        ], 'urlencoded', 'GET');
    }

    public function getList($page, $limit) {
        $query = new self();

        $paginate = $query->paginate($limit, $this->fields, 'page', $page);
        $paginate = collect($paginate)->toArray();
        if(empty($paginate['data'])) return $paginate;

        $hostUserIdArr = array_column($paginate['data'], 'host_id');
        $sql = 'select `user_id`,`nickname`,`uuid` from `zb_user` where user_id in(%s)';
        $sql = sprintf($sql, implode(',', $hostUserIdArr));
        $users = DB::connection('api')->select($sql);
        $users = $this->objToArray($users);
        // $userNicknameMap = array_column($users, null, 'user_id');

        $roomIdArr = array_column($paginate['data'], 'room_id');
        $sql = 'select `room_id`,`name`,`room_uid` from `zb_room` where room_id in(%s)';
        $sql = sprintf($sql, implode(',', $roomIdArr));
        $rooms = DB::connection('api')->select($sql);
        $roomNameMap = array_column($rooms, null, 'room_id');
        $channel = $this->getChannelList();
        $channelNameMap = array_column($channel, 'name', 'id');
        
        foreach($paginate['data'] as &$item) {
            // $item['follow_name'] = $userNicknameMap[$item['host_id']]->nickname ?? '';
            // $item['follow_uid'] = $userNicknameMap[$item['host_id']]->uuid ?? '';
            $item['room_name'] = $roomNameMap[$item['room_id']]->name ?? '';
            $item['room_uid'] = $roomNameMap[$item['room_id']]->room_uid ?? '';
            $item['channel_name'] = $channelNameMap[$item['platform_id']] ?? '';
        }
        unset($item);
        return $paginate;
    }

    public function getChannelList() {
        $list = DB::connection('api')->select('select * from zb_third_channels');
        return $this->objToArray($list);
    }
}
